$(function () {
	var area_box = $('.area_box');
	initAreaSelect(area_box);

	area_box.on('change', '.province', function () {
		setProvince(area_box, $(this).val());
		setCity(area_box, 0);
		setcountry(area_box, 0);
	});
	area_box.on('change', '.city', function () {
		setCity(area_box, $(this).val());
		setcountry(area_box, 0);
	});
});

function initAreaSelect(area_box) {
	var province_id = area_box.find('.province').attr('_data_id');
	var city_id = area_box.find('.city').attr('_data_id');
	var country_id = area_box.find('.country').attr('_data_id');
	setProvince(area_box, province_id);
	setCity(area_box, city_id);
	setcountry(area_box, country_id);
}

function setProvince(area_box, province_id) {
	var province = area_box.find('.province');
	var province_html = '<option value="">省</option>';
	for (var i in province_json) {
		var selected = '';
		if (province_id && province_id==province_json[i].id) {
			selected = 'selected';
		}
		province_html += '<option value="' + province_json[i].id + '" ' + selected + '>' + province_json[i].area_name + '</option>';
	}
	province.html(province_html);
}

function setCity(area_box, city_id) {
	var city = area_box.find('.city');
	var province = area_box.find('.province');
	var city_html = '<option value="">市</option>';
	var province_id = province.val();
	if (province_id) {
		for (var i in city_json) {
			if (province_id==city_json[i].tid) {
				var selected = '';
				if (city_id && city_id==city_json[i].id) {
					selected = 'selected';
				}
				city_html += '<option value="' + city_json[i].id + '" ' + selected + '>' + city_json[i].area_name + '</option>';
			}
		}
	}
	city.html(city_html);
}

function setcountry(area_box, country_id) {
	var city = area_box.find('.city');
	var country = area_box.find('.country');
	var country_html = '<option value="">区</option>';
	var city_id = city.val();
	if (city_id) {
		for (var i in country_json) {
			if (city_id==country_json[i].tid) {
				var selected = '';
				if (country_id && country_id==country_json[i].id) {
					selected = 'selected';
				}
				country_html += '<option value="' + country_json[i].id + '" ' + selected + '>' + country_json[i].area_name + '</option>';
			}
		}
	}
	country.html(country_html);
}